<template>
  <div class="permission">
    <el-card>
      <div style="text-align: right">
        <el-button
          type="primary"
          class="btn"
          @click="add('0', 1)"
        >添加菜单</el-button>
      </div>
      <el-table :data="list" row-key="id">
        <el-table-column label="菜单名称" prop="name" />
        <el-table-column label="权限标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作" prop="">
          <template v-slot="{ row }">
            <div>
              <el-button
                type="text"
                @click="add(row.id, 2)"
              >添加权限点</el-button>
              <el-button
                type="text"
                @click="add(row.id, 1)"
              >添加页面</el-button>
              <el-button type="text" @click="emit(row)">修改</el-button>
              <el-button type="text" @click="del(row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <Dialog ref="dialog" v-model="show" @getData="getData" />
  </div>
</template>

<script>
import { SysPermission, SysPermissionDel } from '@/api/permission.js'
import { changeTreeData } from '@/utils/index.js'
import Dialog from './components/dialog.vue'
export default {
  components: {
    Dialog
  },
  data() {
    return {
      list: [],
      show: false
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await SysPermission()
      this.list = changeTreeData(res.data, '0')
      console.log(res)
    },
    del(row) {
      this.$confirm(`你确定要删除${row.name}的内容?`, '提示')
        .then(async() => {
          const res = await SysPermissionDel(row.id)
          this.$message.success('删除成功')
          this.getData()
          console.log('删除', res)
        })
        .catch(() => {})
    },
    add(pid, type) {
      this.show = true
      this.$refs.dialog.form.type = type
      this.$refs.dialog.form.pid = pid
    },
    emit(row) {
      this.show = true
      this.$refs.dialog.form = JSON.parse(JSON.stringify(row))
      this.$refs.dialog.mode = 'emit'
    }
  }
}
</script>

<style lang="scss" scoped>
.permission {
  padding: 15px;
}
.btn {
  height: 30px;
  padding: 10px;
  line-height: 10px;
}
</style>
